Imports System.Collections

Partial Public Class pages_files_usercontrols_UCFile : Inherits UserControl

    Private m_file As CFile

#Region "Interface"
    Public Sub Display(ByVal [file] As CFile, sortedList As CFileList, showProduct As Boolean)
        If Parent.Controls.Count Mod 2 = 0 Then row.Attributes.Add("class", "alt_row")

        colProduct.Visible = showProduct
        colDelete.Visible = Not showProduct

        m_file = file

        With [file]
            litNumber.Text = CStr(sortedList.IndexOf(file) + 1)
            lnkFileProductId.Text = String.Concat(.Product.SpeakerName, ": ", .Product.ProductName)
            lnkFileProductId.ToolTip = .Product.ProductName
            lnkFileProductId.NavigateUrl = CSitemap.ProductFiles(.FileProductId)

            lnkFileName.Text = .FileNameShort
            lnkFileName.ToolTip = .FileName
            lnkFileName.NavigateUrl = CConfig.DropboxUrl(.FileName)

            If Not IsNothing(.Extension) Then
                With .Extension
                    lblExtension.Text = .ExtensionName
                    lblExtension.ToolTip = String.Concat(.ExtensionLookFor, vbCrLf, .ExtensionBehaviours)
                End With
            End If

            If .FileSize <= 0 Then
                btnVerify.Visible = True
            Else
                litFileSize.Text = CUtilities.FileSize(.FileSize)
            End If
            litFileSize.ToolTip = .FileSize.ToString("n0") & " bytes"
        End With
    End Sub
#End Region

    Protected Sub btnDelete_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles btnDelete.Click
        m_file.Delete()
        Response.Redirect(Request.RawUrl)
    End Sub

    Protected Sub btnVerify_Click(sender As Object, e As System.EventArgs) Handles btnVerify.Click
        Try
            Dim wc As New Net.WebClient
            Dim b As Byte() = wc.DownloadData(CConfig.DropboxUrl(m_file.FileName))
            m_file.FileSize = b.Length
            m_file.Save()
        Catch ex As Exception
            CWeb.Alert(Page, "File not found - check path")
        End Try
        Response.Redirect(Request.RawUrl)
    End Sub
End Class

